#mbrot.py
import numpy as np
import time
import matplotlib.pyplot as plt

#生成z坐标，axis为起始位置，nx，ny为x向和y向的格点个数
def genZ(axis,nx,ny):
    x0, x1, y0, y1 = axis
    x = np.linspace(x0, x1, nx)
    y = np.linspace(y0, y1, ny)
    real, img = np.meshgrid(x, y)
    z = real + img * 1j
    return z
#获取Julia集，n为迭代次数，m为判定发散点，大于1即可
def getJulia(z, c, n, m = 2):
    t = time.time()
    c = np.zeros_like(z) + c
    out = abs(z)
    for i in range(n):
        absz = abs(z)
        z[absz>m] = 0		#对开始发散的点置零
        c[absz>m] = 0		
        out[absz>m] = i	#记录发散点的发散速度
        z = z * z + c
    print("time:",time.time() - t)
    return out
if __name__ == "__main__":
    axis = np.array([-2, 1, -1.5, 1.5])
    z0 = genZ(axis,500, 500)
    mBrot = getJulia(z0, z0, 100)
    plt.imshow(mBrot, cmap=plt.cm.jet, extent=axis)
    # plt.gca().set_axis_off()
    plt.show()
